<template>
	<view>
		<sidebar></sidebar>
		<view class="main-box">
			<view class="top-title">{{$t('profile.profile_info')}}</view>
			<view class="info-box">
				<view class="info-text">
					<view class="info-title">{{$t('profile.company')}}</view>
					<view class="input-box">
						<u-input v-model="userInfo.company" class="val_box"></u-input>
					</view>
				</view>
				<view class="info-edit" style="margin-top: 20px;">
					<u-icon name="edit-pen" size="24" @click="postData('company')"></u-icon>
				</view>
			</view>
			<view class="info-box">
				<view class="info-text">
					<view class="info-title">{{$t('profile.email')}}</view>
					<view class="input-box">
						<u-input v-model="userInfo.email" class="val_box"></u-input>
					</view>
				</view>
				<view class="info-edit" style="margin-top: 20px;">
					<u-icon name="edit-pen" size="24" @click="postData('email')"></u-icon>
				</view>
			</view>
			<view class="info-box">
				<view class="info-text">
					<view class="info-title">{{$t('profile.first_name')}}</view>
					<view class="input-box">
						<u-input v-model="userInfo.first_name" class="val_box"></u-input>
					</view>
				</view>
				<view class="info-edit" style="margin-top: 20px;">
					<u-icon name="edit-pen" size="24" @click="postData('first_name')"></u-icon>
				</view>
			</view>
			<view class="info-box">
				<view class="info-text">
					<view class="info-title">{{$t('profile.last_name')}}</view>
					<view class="input-box">
						<u-input v-model="userInfo.last_name" class="val_box"></u-input>
					</view>
				</view>
				<view class="info-edit" style="margin-top: 20px;">
					<u-icon name="edit-pen" size="24" @click="postData('last_name')"></u-icon>
				</view>
			</view>
			<view class="info-box">
				<view class="info-text">
					<view class="info-title">{{$t('profile.phone_number')}}</view>
					<view class="tips-box">
						<!-- {{$t('profile.tips')}} -->
						{{userInfo.mobile}}
					</view>
				</view>
				<view class="info-add" @click="handlePop">
					{{$t('profile.add')}} <u-icon name="plus" size="20" color="#f8e801"></u-icon>
				</view>
			</view>
			<view class="info-box">
				<view class="info-text">
					<view class="info-title">{{$t('profile.birthday')}}</view>
					<view class="tips-box">
						<!-- {{$t('profile.tips')}} -->
						{{userInfo.birthday}}
					</view>
				</view>
				<view class="info-add" @click="showDateSelect = true">
					{{$t('profile.add')}} <u-icon name="plus" size="20" color="#f8e801"></u-icon>
				</view>
			</view>
			<view class="info-box">
				<view class="info-text">
					<view class="info-title">{{$t('profile.gender')}}</view>
				</view>
				<view class="info-add">
					<view :class="userInfo.sex==1?'sex-label sex-label-on':'sex-label'" @click="setSex(1)">
						<u-icon name="plus" size="14" color="#666"></u-icon>
						{{$t('profile.male')}}
					</view>
					<view :class="userInfo.sex==2?'sex-label sex-label-on':'sex-label'" @click="setSex(2)">
						<u-icon name="plus" size="14" color="#666"></u-icon>
						{{$t('profile.female')}}
					</view>
				</view>
			</view>
			<!-- <view class="info-box">
			<view class="info-text">
				<view class="info-title">{{$t('profile.nationality')}}</view>
				<view class="tips-box" style="color: $main;font-size: 32rpx;font-weight: 700;">
					{{$t('profile.selectNationality')}}
				</view>
			</view>
			<view class="info-add" @click="handlePop(3)">
				{{$t('profile.add')}} <u-icon name="plus" size="20" color="#f8e801"></u-icon>
			</view>
		</view> -->
			<u-popup :show="showPop" @close="showPop=false">
				<view>
					<view class="pop-title">
						<view>{{$t('profile.add_phoneNumber')}}</view>
						<view class="close" @click="showPop=false">×</view>
					</view>
					<view class="pop-input-box">
						<view class="pop-sub-title">{{$t('profile.tips1')}}</view>
						<view class="pop-input-title">{{$t('profile.tips2')}}</view>
						<view>
							<u-input v-model="userInfo.mobile">
								<template #prefix>
									<view @click="showSelectPhone=true" class="phone-select">
										<view>{{phone}}</view>
										<view>
											<u-icon name="arrow-down"></u-icon>
										</view>
									</view>
								</template>
							</u-input>
						</view>
						<view style="margin-top: 35rpx;">
							<u-button :plain="false" :text="$t('profile.add_phone_button')" @click="handleMobileConfirm"
								style="background-color: $main;color:#000"></u-button>
						</view>
					</view>
				</view>
			</u-popup>
			<u-picker :cancelText="$t('operateBtn.cancelBtn')" :confirmText="$t('operateBtn.sureBtn')" :show="showSelectPhone" :columns="phonePrefix" @close="showSelectPhone=false"
				@cancel="showSelectPhone=false" :closeOnClickOverlay="true" @confirm="handlePhoneConfirm"></u-picker>
			<u-datetime-picker :show="showDateSelect" v-model="userInfo.birthday" mode="date"
				@close="showDateSelect=false" @cancel="showDateSelect=false" :closeOnClickOverlay="true"
				@confirm="handleBirthdayConfirm" :minDate="minDate"></u-datetime-picker>
		</view>
	</view>
</template>

<script>
	import sidebar from '@/components/sidebar.vue'
	export default {
		components: {
			sidebar
		},
		data() {
			return {
				userInfo: {},
				showPop: false,
				showSelectPhone: false,
				phone: '+966',
				
				phonePrefix: [
					['+966','+971','+973','+968','+965', '+974']
				],
				showDateSelect: false,
				minDate: '',
			}
		},
		onLoad() {
			this.userInfo = this.$cache.get('userInfo')
			const now = new Date()
			this.minDate = now.setFullYear(now.getFullYear() - 90)
		},
		onShow(){
			uni.setNavigationBarTitle({
				title:this.$t('pages.profile'),
			})
		},
		onPullDownRefresh() {
			this.getUserInfo();
			
			uni.stopPullDownRefresh(); // 停止下拉刷新动画
		},
		methods: {
			handlePop() {
				this.showPop = true
			},
			handlePhoneConfirm(e) {
				this.phone = e.value[0]
				this.showSelectPhone = false
			},
			postData(field) {
				const val = this.userInfo[field];
				if (val == '') {
					uni.showToast({
						title: this.$t('请填写内容'),
						icon: 'none'
					})
					return
				}
				this.$api('/user/changeinfo', {
					[field]: val
				}).then(res => {
					this.getUserInfo()
				})
			},
			getUserInfo() {
				this.$api('/user/index').then(res => {
					this.userInfo = res
					this.$cache.set('userInfo', res)
				})
			},
			handleMobileConfirm(e) {
				console.log(e);
				this.postData('mobile')
				this.showPop = false
			},
			handleBirthdayConfirm(e) {
				const date = new Date(e.value); // 将时间戳转换为Date对象
				const birthday = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
				console.log('birthday', birthday)
				this.userInfo.birthday = birthday
				this.postData('birthday')
				this.showDateSelect = false
			},
			setSex(type) {
				this.userInfo.sex = type
				this.postData('sex')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main-box {
		padding: 20rpx;
		color: #666;

		.top-title {
			font-size: 36rpx;
			font-weight: 700;
		}
		
		.info-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 45rpx;

			.info-title {
				font-size: 32rpx;
				margin-bottom: 10rpx;
			}

			.val_box {
				width: 600rpx;
				font-size: 32rpx;
				font-weight: 700;
				
			}

			.tips-box {
				font-size: 26rpx;
				color: #999;
			}

			.info-add {
				display: flex;
				align-items: center;
				font-size: 32rpx;
				color: $main;
				font-weight: 700;
			}

			.sex-label {
				display: flex;
				align-items: center;
				padding: 10rpx 15rpx;
				margin-left: 10rpx;
				border: 1px #ccc solid;
				background-color: #f1f1f1;
				border-radius: 30rpx;
				font-size: 28rpx;
				color: #666;
			}

			.sex-label-on {
				background-color: #bfbfbf;
			}

		}
	}

	.pop-title {
		display: flex;
		justify-content: space-between;
		padding: 35rpx;
		font-weight: 700;
		font-size: 36rpx;
		border-bottom: 1px #f1f1f1 solid;
	}

	.pop-input-box {
		padding: 30rpx;
	}

	.pop-sub-title {
		display: flex;
		justify-content: center;
		padding: 45rpx 0;
		font-size: 30rpx;
	}

	.pop-input-title {
		font-size: 30rpx;
		font-weight: 700;
		margin-bottom: 15rpx;
	}

	.phone-select {
		display: flex;
		align-items: center;
	}
</style>